<?php
/* Template Name: 文章归档 
 * Author: Cleris
 */
 ?>
<?php get_header()?>
<div class="hide"><?php ini_map()?></div>
<?php
class ini_archives{
	function GetPosts(){
		global $wpdb; if ( $posts = wp_cache_get('posts', 'ini-clean-archives') ) return $posts;
		$query="SELECT DISTINCT ID, post_date, post_date_gmt, comment_count, comment_status, post_password FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' AND comment_status = 'open'";
		$rawposts =$wpdb->get_results( $query, OBJECT );
		foreach( $rawposts as $key=>$post ){
			$posts[ mysql2date( 'Y.m', $post->post_date ) ][] = $post; $rawposts[$key] = null;
		}
		$rawposts = null; wp_cache_set( 'posts', $posts, 'ini-clean-archives' ); return $posts;
	}
	function PostList( $atts = array() ){
		global $wp_locale, $clean_config;
		$atts = shortcode_atts( array('usejs'=>$clean_config['usejs'], 'monthorder'=>$clean_config['monthorder'], 'postorder'=>$clean_config['postorder'], 'postcount'=>'1', 'commentcount'=>'1'),  $atts );
		$atts = array_merge( array( 'usejs'=>1, 'monthorder'=>'new', 'postorder'=>'new'), $atts );
		$posts = $this->GetPosts();
		('new' == $atts['monthorder'] )? krsort( $posts ): ksort( $posts );
		foreach( $posts as $key=>$month ){
			$sorter = array();
			foreach ( $month as $post ) $sorter[] = $post->post_date_gmt;
			$sortorder = ( 'new' == $atts['postorder'] )? SORT_DESC: SORT_ASC; array_multisort( $sorter, $sortorder, $month );
			$posts[$key] = $month;
			unset($month);
		}
// Output
		if ( 1 == $atts['usejs'] ) $html ="<div class='car-list'>\n";
		foreach( $posts as $yearmonth=>$posts ){
			list( $year, $month ) = explode( '.', $yearmonth );
			$firstpost = TRUE;
			$i = 0; foreach( $posts as $post ){
				$year = mysql2date( 'Y', $post->post_date );
				$month = mysql2date( 'm', $post->post_date );
				$day = mysql2date( 'd', $post->post_date );
				if( $i < 6 ){
					$i++;
					if ( TRUE == $firstpost ){
						$html.= "<button>".sprintf( __(' %1$s %2$d'), $wp_locale->get_month($month), $year )." (共".count($posts)."篇文章)</button>\n<ul class='car-monthlist'>\n";
						$firstpost = FALSE;
					}
					$html.= "<li><span>{$month}月{$day}<i>&hearts;</i></span><p><a href=\"" .get_permalink( $post->ID ). "\" rel=external >" .get_the_title( $post->ID ). "</a>";
					if ( 0!= $atts['commentcount']&&( 0!= $post->comment_count||'closed'!=$post->comment_status )&&empty($post->post_password) )
					$html.= " &nbsp;(".$post->comment_count."条评论)</p></li>\n";
				} else if( $i == 6 ) {
					$i++;
					$html.=  '<li><span>'. $month . '月文<i>&hearts;</i></span><a href="' .get_month_link($year, $month). '" rel=external >未加载完，点击查看更多 >>></a>';
				}
			}
			$html.= "</ul>\n";
		}
		$html.= "</div>\n";
		return $html;
	}
	function PostCount(){ return wp_count_posts('post')->publish; }
} // end ini_archives
if( !empty($post->post_content) ){
	$all_config=explode(';', $post->post_content);
	foreach($all_config as $item){ $temp=explode('=', $item); $clean_config[trim($temp[0])]=htmlspecialchars( strip_tags(trim($temp[1])) );}
} else {
	$clean_config=array('usejs'=>1, 'monthorder'=>'new', 'postorder'=>'new');
}
$ini_archives = new ini_archives();


//导航菜单
	$custom_fields = get_post_custom_keys($post_id);
	if( !in_array( 'pgmenu', $custom_fields) ){
		$pgmenu = 1;
	}
?>
	<div class="main-page" <?php if( $pgmenu!==1 ) echo 'style="padding:20px 25px"'?>>
		<?php if( $pgmenu==1 && !ismobile() ) wp_nav_menu( array('theme_location' => 'pagemenu', 'class' => 'pg-menu') )?>
		<h2 class="entry-title"><?php the_title()?></h2>
		<span><?php edit_post_link('编辑文章', '<i class="icon-edit"></i> ')?></span>
		<?php the_content()?>
		<form class="cy-search car-search" method="get" action="<?php bloginfo('home'); ?>">
			<input type="text" name="s" value="" placeholder="搜索<?php echo $ini_archives->PostCount()?>篇文章"/>
			<input type="submit" class="icon-search" value="<?php echo ismobile() ? "&#xe011;" : "&#xf002;"?>"/>
		</form>
		<div property="v:description" style=position:relative ><?php echo $ini_archives->PostList()?></div>
	</div>
<?php get_footer()?>